//    Copyright (C) Kherty.  All rights reserved.
using System.Diagnostics.Contracts;
using System.Windows;
using System.Windows.Controls;
using System.Diagnostics;


namespace OpenLS.Drawing.UI
{
    partial class FillResources
    {
        public FillResources()
        {
            InitializeComponent();
        }
        void OnRadioButtonChecked(object sender, RoutedEventArgs args)
        {
            ShapeProperties sp =  DrawingElement.GetShapeProperties((DependencyObject) sender);
            if (sp == null)
                return;
            FrameworkElement rb = (FrameworkElement) args.Source  ;
            switch (rb.Name)
            {
                case "noFill":
                    sp.FillProperties = new NoFillProperties();
                    break;
                case "automatic":
                    sp.FillProperties = null;
                    break;
                case "gradientFill":
                    if (!(sp.FillProperties is GradientFillProperties))
                        sp.FillProperties = new GradientFillProperties();
                    break;

            }
        }
        void OnLoaded(object sender, object args)
        {
            FrameworkElement element = (FrameworkElement) sender;
            ShapeProperties sp = DrawingElement.GetShapeProperties((DependencyObject) sender);
            if (sp == null)
            {
                sp = (ShapeProperties) element.DataContext;
                if (sp == null)
                    return;
                DrawingElement.SetShapeProperties(element, sp);
            }
            if (sp.FillProperties == null)
            {
                GetRadioButton("automatic", sender).IsChecked = true;
                return;
            }
            if (sp.FillProperties is NoFillProperties)
            {
                GetRadioButton("noFill", sender).IsChecked = true;
                return;
            }
            if (sp.FillProperties is SolidColorFillProperties)
            {
                GetRadioButton("solidFill", sender).IsChecked = true;
                return;
            }
            if (sp.FillProperties is GradientFillProperties)
            {
                GetRadioButton("gradientFill", sender).IsChecked = true;
                return;
            }
            if (sp.FillProperties is PatternFillProperties)
            {
                GetRadioButton("pictureFill", sender).IsChecked = true;
                return;
            }
            // Contract.Assert(false);
        }
        static RadioButton GetRadioButton(string name, object sender)
        {
            ContentPresenter cp = (ContentPresenter)((FrameworkElement)sender).TemplatedParent;
            DataTemplate dt = cp.ContentTemplate;
            return dt.FindName(name, cp) as RadioButton;
        }
        void OnSolidFillChanged(object sender, SelectionChangedEventArgs args)
        {
            FrameworkElement fSender = (FrameworkElement)sender;
            if (fSender.Visibility != Visibility.Visible)
                return;
            RadioButton rb = fSender.Tag as RadioButton;
            if (rb == null || !rb.IsChecked.GetValueOrDefault())
                return;
            SolidColorFillProperties c = new SolidColorFillProperties((ColorBase)args.AddedItems[0]);
            ShapeProperties s = DrawingElement.GetShapeProperties(fSender);
            if (s != null)
                s.FillProperties = c;
        }

    }
}
